package parse_and_index.search.inverted;

import java.util.HashMap;

import parse_and_index.build.inverted.Stemmer;
import parse_and_index.model.TermFrequency;

public class Query {
	  private final HashMap<String, TermFrequency> queryMap;

	  public Query(final String term) {
			queryMap = new HashMap<String, TermFrequency>();
			init(term);
	  }

	  public HashMap<String, TermFrequency> getQueryTerms() {
			return queryMap;
	  }

	  private void init(final String term) {
			final String[] temp = term.split(" ");
			for (final String element : temp) {
				  if (element.length() < 3) {
						continue;
				  }
				  addNewQueryTerm(element);
			}
	  }

	  private void addNewQueryTerm(final String queryTerm) {
			final Stemmer stemmer = new Stemmer(queryTerm);
			stemmer.UTF8CyrillicToLatin();
			stemmer.stem();
			TermFrequency termFrequency = null;
			if (queryMap.containsKey(stemmer.getTerm().toLowerCase())) {
				  termFrequency = queryMap.get(stemmer.getTerm().toLowerCase());
				  queryMap.remove(stemmer.getTerm().toLowerCase());
				  termFrequency.incrementTermFrequency();
			}
			else {
				  termFrequency = new TermFrequency();
			}
			queryMap.put(stemmer.getTerm().toLowerCase(), termFrequency);
	  }

	  @Override
	  public String toString() {
			return queryMap.toString();
	  }
}